//
//  fast-convert.swift
//  jocool
//
//  Created by tong on 16/6/29.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Foundation

public struct Display {
    static public func distance(_ distance:Any) -> String{//m为传入参数单位
        var value:CGFloat = 0
        
        if let v = distance as? CGFloat{
            value = v
        }
        else if let v = distance as? String{
            value = v.CGFloatV
        }
        if value <= 0{
            return ""
        }
        if value.km <= 1.0
        {
            return value.print(num: 0) + " m"
        }else{
            return value.km.print(num: 1) + " km"

        }
    }
}

//fileprivate let __swidth = UIScreen.main.bounds.width

extension Int
{
    
    public var co:CGFloat{
        get{
            return (self.cg_floatValue/__width)*Swidth
        }
    }
    public var fs:CGFloat{
        get{
            if CGFloat.autoSizeFsUni{
                return self.cg_floatValue * (Swidth/__width)
            }
            return self.cg_floatValue
        }
    }
}



extension CGFloat
{
    public static var autoSizeFsUni:Bool = false
    public var km: CGFloat { return self / 1_000.0 }
    public var m : CGFloat { return self }
    public var cm: CGFloat { return self * 100.0 }
    public var mm: CGFloat { return self * 1_000.0 }
    public var ft: CGFloat { return self * 3.28084 }
    public var fs: CGFloat {
        if CGFloat.autoSizeFsUni{
            return self * (Swidth/__width)
        }
        return self
    }

}


extension CGFloat
{
    public func print(num point_num:Int) ->String
    {
        return  NSString(format: "%.\(point_num)f" as NSString, self) as String
    }
}

extension String {
    public var len: Int { return self.count }
    
}


extension NSMutableDictionary{
    public var swift_dictionary:[String:Any]
    {
        get{
            var dict = [String:Any]()
            for _value in self.enumerated()
            {
                dict["\(_value.element.key)"] = _value.element.value
            }
            return dict
        }
    }
}

extension NSMutableArray{
    public var swift_array:[[String:Any]]
    {
        get{
            var array = [[String:Any]]()
            for _value in self
            {
                if let d = _value as? [String:Any]{
                    array.append(d)
                }else if let d = _value as? NSMutableDictionary{
                    array.append(d.swift_dictionary)
                }
            }
            return array
        }
    }
}



extension DispatchQueue {
    static var Interactive: DispatchQueue { return DispatchQueue.global(qos: .userInteractive) }
    static var Initiated: DispatchQueue { return DispatchQueue.global(qos: .userInitiated) }
    static var Utility: DispatchQueue { return DispatchQueue.global(qos: .utility) }
    static var Background: DispatchQueue { return DispatchQueue.global(qos: .background) }
    
    func After(_ delay: TimeInterval, execute closure: @escaping () -> Void) {
        asyncAfter(deadline: .now() + delay, execute: closure)
    }
    
    func SyncResult<T>(_ closure: () -> T) -> T {
        var result: T!
        sync { result = closure() }
        return result
    }
}



